package com.note.controller;

import com.alibaba.fastjson.JSON;
import com.note.entity.User;
import com.note.service.UserService;
import com.note.servlet.Response;
import com.note.servlet.ResponseMessage;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * Created by XQ on 2017/6/3.
 */
@RestController
public class UserController {
    private static final Logger LOGGER = Logger.getLogger(UserController.class);
    @Autowired
    UserService userService;

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/users", method = RequestMethod.POST)
    public String register(@RequestBody User user) {

        ResponseMessage responseMessage = new ResponseMessage();
        Response response = null;

        //查询登录的账户
        User user1 = userService.isExistUser(user.getUserAccount());

        //判断用户账户是否存在
        if (user1 == null) {
            userService.register(user);
            response = responseMessage.ResponseMessage(user.getUserAccount(), 1, "注册成功！");
        } else {
            response = responseMessage.ResponseMessage(null, 0, "用户已存在！");
        }
        return JSON.toJSONString(response);
    }

    /**
     * 用户登录
     *
     * @param user
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String logIn(@RequestBody User user) {

        ResponseMessage responseMessage = new ResponseMessage();
        Response response = null;

        User user1 = userService.findUserByUserAccount(user.getUserAccount());
        if (user1.getUserAccount().equals(user.getUserAccount()) && user1.getPassword().equals(user.getPassword())) {
            response = responseMessage.ResponseMessage(user.getUserAccount(), 1, "登录成功！");
        } else {
            response = responseMessage.ResponseMessage(user.getUserAccount(), 1, "密码或者账号错误！");
        }
        return JSON.toJSONString(response);
    }

    /**
     * 得到用户信息
     *
     * @param userAccount
     * @return
     */
    @RequestMapping(value = "/users/{userAccount}", method = RequestMethod.GET)
    public User getOneUserInfo(@PathVariable String userAccount) {
        return userService.findUserInfoByUserAccount(userAccount);
    }

    /**
     * 更新用户的头像
     * @param file
     * @param request
     * @throws IllegalStateException
     * @throws IOException
     */
    @RequestMapping(value = "/photoUpload", method = RequestMethod.POST)
    public void photoUpload(MultipartFile file, HttpServletRequest request) throws IllegalStateException,IOException {
        if (file != null) {// 判断上传的文件是否为空
            String path = null;// 文件路径
            String type = null;// 文件类型
            String fileName = file.getOriginalFilename();// 文件原名称
//            System.out.println("上传的文件原名称:" + fileName);
            // 判断文件类型
            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {// 判断文件类型是否为空
                if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
                    // 项目在容器中实际发布运行的根路径
                    String realPath = request.getSession().getServletContext().getRealPath("/resources/img/");
                    // 自定义的文件名称
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
                    // 设置存放图片文件的路径
                    path = realPath +/*System.getProperty("file.separator")+*/trueFileName;
//                    System.out.println("存放图片文件的路径:" + path);
                    // 转存文件到指定的路径
                    file.transferTo(new File(path));
//                    System.out.println("文件成功上传到指定目录下");
                } else {
//                    System.out.println("不是我们想要的文件类型,请按要求重新上传");
                }
            } else {
//                System.out.println("文件类型为空");
            }
        } else {
//            System.out.println("没有找到相对应的文件");
        }
    }
}
